S
@livius
As previous I intend SiPy-1.6.7.b1.tar
I can post you the entire code, but this could be used only with our device connected to the UART serial port.
Anyway, I post you the code used to handle sockets, because I suspect that the issue could be
related to this task:
import _thread
import tracingfunction
import time
import socket
import pycom
# Global array for rx data from serial port
SerialRxPacketsLock = _thread.allocate_lock()
# this value define the max number of parallel socket!
# Don't configure a value grather than the maximum allowable connections
# on uart device!! (see the define MAX_SOCK_NUM in the uart device project)
MaxNumActiveSockets = 6
# this arry is used by the task serialport to request a close on the relative socket
KillRequestSocketList = bytearray(MaxNumActiveSockets)
# this mutex is used to avoid thread conflicts in th access of the list of packets and socket
SocketListLock = _thread.allocate_lock()
# list of packets that must be sent on a particular socket
rxPacketsFromSerialPortList = []
# list of enabled sockets
socketList = []
# list of id related to the socket!
socketUniqueIdList = bytearray(MaxNumActiveSockets)
pycom.heartbeat(False)
currentRGBLed = 0x007f00
# this function return the number of current active sockets
def GetNumOfActiveSocketList():
global SocketListLock
global socketList
global MaxNumActiveSockets
idx=0
totalActiveSocket=0
SocketListLock.acquire()
while(idx<MaxNumActiveSockets):
if(socketList[idx]!=None):
totalActiveSocket=totalActiveSocket + 1
idx = idx + 1
SocketListLock.release()
return totalActiveSocket
# this function return the position of the first free socket slot
def ObtainSocketListPosition():
global SocketListLock
global MaxNumActiveSockets
idx=0
SocketListLock.acquire()
while(idx<MaxNumActiveSockets):
if(socketList[idx]==None):
break
idx = idx + 1
if(idx==MaxNumActiveSockets):
idx=-1
SocketListLock.release()
return idx
def toggleRGBLed():
global currentRGBLed
if(currentRGBLed==0x007f00):
currentRGBLed=0
else:
currentRGBLed=0x007f00
pycom.rgbled(currentRGBLed)
# socketThread
# This task is responsible to handle data from/to the socket
# ------------------------------------------------------------------------------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------------------------------------------------------
def socketThread(voidPar):
import stuffingfunctions
global socketList
global SerialRxPacketsLock
global rxPacketsFromSerialPortList
global KillRequestSocketList
# we print on the terminal that the thread started
print ("socketThread Started")
# set counters and ids to zero
idxSocketToCheck = 0
aliveCounter = 0
pycom.rgbled(0x00007f)
# infinite loop!
while True:
# increase the alive counter
aliveCounter = aliveCounter + 1
# if the alive counter reachs 15000, print on the
# terminal that this thread is alive
if(aliveCounter>15000):
aliveCounter = 0
print("socketThread alive!!")
# assign the thread socket position
threadIdPos = idxSocketToCheck
# if there is a kill request, the buffer is empty and the socket is valid, close the socket
if(KillRequestSocketList[threadIdPos]==1) and (socketList[threadIdPos]!=None) and (rxPacketsFromSerialPortList[threadIdPos]==None):
socketList[threadIdPos].close()
socketList[threadIdPos]=None
KillRequestSocketList[threadIdPos]=0
print("Socket " + str(threadIdPos) + " killed!")
# acquire the mutex
SerialRxPacketsLock.acquire()
if (socketList[threadIdPos]!=None):
# if the current socket is alive
if(rxPacketsFromSerialPortList[threadIdPos]==None):
# if there is no data to send, do nothing
pass
else:
# if there is data to send, try to send it
try:
print("Writing data into socket " + str(threadIdPos))
socketList[threadIdPos].send(rxPacketsFromSerialPortList[threadIdPos])
print("Write data ok")
rxPacketsFromSerialPortList[threadIdPos]=None
toggleRGBLed()
# pycom.rgbled(0x007f00)
except:
# in case of exception, trace on file and close the socket
print("Exception sending data " + str(threadIdPos))
tracingfunction.TraceLine("Exception sending data to the socket!")
socketList[threadIdPos].close()
socketList[threadIdPos]=None
stuffingfunctions.WriteStuffingSerialPort(None, 0,threadIdPos, 1, socketUniqueIdList[threadIdPos])
rxPacketsFromSerialPortList[threadIdPos]=None
else:
# if the current socket is not alive, drop the data...
rxPacketsFromSerialPortList[threadIdPos]=None
# release the mutex
SerialRxPacketsLock.release()
if (socketList[threadIdPos]!=None):
# if the current socket is alive
# set partialData to None
partialData=None
while True:
try:
data=None
#Receiving from client
data = socketList[threadIdPos].recv(1460)
if not data:
# if we have not received anitying
if (partialData!=None):
# in this case, if we have partial data != none, the data is only in the partialData array
data=partialData
print("Received data from socket " + str(threadIdPos) + " Len: " + str(len(data)))
stuffingfunctions.WriteStuffingSerialPort(data, len(data),threadIdPos, 0 , socketUniqueIdList[threadIdPos])
toggleRGBLed()
# pycom.rgbled(0x00007f)
break
else:
if(len(data)>0):
# if we have received something
if(len(data)==1024):
# in this case, there are probably many bytes to read (python slices packets greather than 1024 in multiple packets)
# we save this data in the partialData and we issue a new read request...
partialData=data
time.sleep(0.002)
continue
else:
if (partialData!=None):
# in this case, if we have partial data != none, we compose the full packet
partialData = partialData + data
data = partialData
else:
# in this case, the data is only in the data array
pass
# we pass the packet to the serial port!
print("Received data from socket " + str(threadIdPos) + " Len: " + str(len(data)))
stuffingfunctions.WriteStuffingSerialPort(data, len(data),threadIdPos, 0 , socketUniqueIdList[threadIdPos])
toggleRGBLed()
# pycom.rgbled(0x00007f)
break
break
except socket.timeout:
if data:
if(len(data)>0):
# added 30/03/2017 10.17 ----->
if (partialData!=None):
# in this case, if we have partial data != none, we compose the full packet
partialData = partialData + data
data = partialData
else:
# in this case, the data is only in the data array
pass
# added 30/03/2017 10.17 <-----
print("Received data from socket " + str(threadIdPos))
stuffingfunctions.WriteStuffingSerialPort(data, len(data),threadIdPos, 0 , socketUniqueIdList[threadIdPos])
toggleRGBLed()
break
except socket.error:
# in case of error exception, we close the socket
print("Closing socket thread " + str(threadIdPos))
if(socketList[threadIdPos]!=None):
socketList[threadIdPos].close()
socketList[threadIdPos]=None
stuffingfunctions.WriteStuffingSerialPort(None, 0,threadIdPos, 1 , socketUniqueIdList[threadIdPos])
break
# we increase the socket idx
idxSocketToCheck = idxSocketToCheck + 1
# eache time we parse all sockets, we do a pause of 2 ms
if idxSocketToCheck==MaxNumActiveSockets:
idxSocketToCheck = 0
time.sleep(0.002)
# we should never reach this point
print ("ThreadRx " + str(threadIdPos) + " terminated!")